package com.ww.springboot.boot.thread.study.base.lock.aqs.demo.lock1;

/**
 * aqs源码学习1
 *
 * @author wanwei
 * @since 2023-03-16 22:21
 */
public class LockDemo {

    public static void main(String[] args) {
        //1.创建一把锁
        //2.创建两个线程
        //3.测试能否实现加锁 解锁

        MySimpleLock myLock = new MySimpleLock();


        new Thread(() -> {
            System.out.println(Thread.currentThread() + "启动:" + System.currentTimeMillis());
            myLock.lock();
            try {
                System.out.println(Thread.currentThread()+"获取锁:" + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (Exception e) {

            } finally {
                System.out.println(Thread.currentThread()+"释放锁:" + System.currentTimeMillis());
                myLock.unlock();
            }
        }).start();

        new Thread(() -> {
            System.out.println(Thread.currentThread() + "启动:" + System.currentTimeMillis());
            myLock.lock();
            try {
                System.out.println(Thread.currentThread()+"获取锁:" + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (Exception e) {

            } finally {
                System.out.println(Thread.currentThread()+"释放锁:" + System.currentTimeMillis());
                myLock.unlock();
            }
        }).start();


    }
}
